<?php

/**
 * 
 */
class Viewreports extends CI_Controller {

    var $area = 0;

    function __construct() {
        parent::__construct();
        $this->load->library('googlemaps');
        $this->load->model('reports_model');
        $this->load->model('areas_model');
    }

    public function index() {
        $config['disableMapTypeControl'] = TRUE;
        $config['disableStreetViewControl'] = TRUE;
        $config['center'] = '48.75,19.70';
        $config['map_height'] = 400;
        $config['sensor'] = false;
        $config['map_width'] = 754;
        $config['zoom'] = '7';
        $this->googlemaps->initialize($config);
        $data['map'] = $this->googlemaps->create_map();

        $content['kk'] = $this->areas_model->returnAllAreas();
        foreach ($content['kk'] as $key => $value) {
            $plus = $this->area++;
            // echo $value['area_id'];
            $content['div'][$plus] = "<div id='area" . $value['area_id'] . "' class='checkbox_route checkbox_route_checked'>" . $value['name'] . "</div>";
        }

        $this->load->view('templates/header', $data);
        $this->load->view('reports/view', $content);
        $this->load->view('templates/footer');
    }

    public function search() {
        $post = $this->input->post();
        $array = $this->reports_model->searchResult($post);
        foreach ($array as $key => $value) {
            $data['image'][$key] = $this->getImage($value['type_id']);
            $data['longitude'][$key] = $value['longitude'];
            $data['latitude'][$key] = $value['latitude'];
            $data['content'][$key] = $value['street'] . "<br />" . $value['city'] . "<br />" . $value['state'];
            $img_up = base_url() . "css/images/up.png";
            $img_down = base_url() . "css/images/down.png";
            $arr_tor = $this->typeOfReport($value['type_id']);
            $link = base_url("report") . "/" . $value['report_id'] . "-" . url_title($value['street_noaccent']) . "-" . url_title($value['city_noaccent']);

            $data['content'][$key] = "<div style='text-align:center;font-weight:bold;padding-bottom: 5px;'>" . $arr_tor['popis'] . "<br/><span style='color:#888811;font-size:11px;font-weight: normal;'>Viditeľné:<br /> " . $this->dateFormatToSlovak($value['seen']) . "</div>Komentáre: " . $value['n_comments'] . " | <img src='$img_up'> " . $value['n_thumb_ups'] . " | <img src='$img_down'> " . $value['n_thumb_downs'] . "<br />Viac o udalosti: <a style='color:red;' href='$link' target='_blank'>tu (v novom okne)</a></div>";
        }
        if (count($array) > 0) {
            echo json_encode($data);
        } else {
            echo json_encode("error");
        }
    }

    private function dateFormatToSlovak($date) {
        return date('d.m.Y H:i', strtotime($date));
    }

    private function typeOfReport($type) {
        switch ($type) {
            case '1':
                $popis = "Radar";
                $obr = "speed.png";
                break;
            case '2':
                $popis = "Policajná hliadka";
                $obr = "police.png";
                break;
            case '3':
                $popis = "Nehoda";
                $obr = "accident.png";
                break;
            case '4':
                $popis = "Mobilný radar";
                $obr = "mobile_radar.png";
                break;
        }
        return array('popis' => $popis, 'obr' => $obr);
    }

    private function getImage($argument) {
        switch ($argument) {
            case 1:
                $image = "speed.png";
                break;
            case 2:
                $image = "police.png";
                break;
            case 3:
                $image = "accident.png";
                break;
            case 4:
                $image = "mobile_radar.png";
                break;
        }

        return $image;
    }

}

?>